package com.xywy.tangandroid.activity;

import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.BaseHttpResponse;
import com.xywy.tangandroid.logic.Task;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.GetRequest;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;

public class RegistAct extends BaseActivity {
	private RelativeLayout login_button;
	private RelativeLayout left_img;
	private TextView middle_title;
	private TextView right_title;
	private ImageView right_img;
	private RelativeLayout forgot_password_button;
	private Button fast_rigist_button;
	// private ImageView username_delete_img;
	// private ImageView pnone_delete_img;
	// private ImageView password_delete_img;
	private EditText phonename;
	private EditText phonenumber;
	private EditText phone_password;
	private String Tag = "RegistAct";
	private int name_flag = 0, phone_flag = 0, password_flag = 0;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Task.REGISTER_VALIDATION:
				if (waittingDialog != null && waittingDialog.isShowing())
					waittingDialog.dismiss();
				BaseHttpResponse response = (BaseHttpResponse) msg.obj;
				if (response != null) {
					if (response.getCode() == 10000) {
						// Intent intent=new Intent(RegistAct.this,
						// ComfrimCodeAct.class);
						// startActivity(intent);
						UIUtil.showToast(RegistAct.this,
								getString(R.string._register_sucessful));
					} else {
						UIUtil.showToast(RegistAct.this, response.getError());
					}
				} else {
					UIUtil.showToast(RegistAct.this,
							getString(R.string._abnormal_register));
				}
				break;
			}
		}
	};

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_fast_register);

		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		right_title = (TextView) findViewById(R.id.right_title);
		right_img = (ImageView) findViewById(R.id.right_img);
		middle_title.setText(getResources().getString(R.string.fastregister));

		phonename = (EditText) findViewById(R.id.phonename);
		phonenumber = (EditText) findViewById(R.id.phonenumber);
		phone_password = (EditText) findViewById(R.id.phone_password);

		fast_rigist_button = (Button) findViewById(R.id.fast_rigist_button);
		// username_delete_img = (ImageView)
		// findViewById(R.id.username_delete_img);
		// pnone_delete_img = (ImageView) findViewById(R.id.pnone_delete_img);
		// password_delete_img = (ImageView)
		// findViewById(R.id.password_delete_img);

		chenck_name_text = (TextView) findViewById(R.id.chenck_name_text);
		chenck_name_img = (ImageView) findViewById(R.id.chenck_name_img);

		chenck_phone_text = (TextView) findViewById(R.id.chenck_phone_text);
		chenck_phone_img = (ImageView) findViewById(R.id.chenck_phone_img);

		chenck_password_text = (TextView) findViewById(R.id.chenck_password_text);
		chenck_password_img = (ImageView) findViewById(R.id.chenck_password_img);

		phonename.addTextChangedListener(NameTextWatcher);
		phonename.setSelection(phonename.length());

		phonenumber.addTextChangedListener(PhoneTextWatcher);
		phonenumber.setSelection(phonenumber.length());

		phone_password.addTextChangedListener(PsswordTextWatcher);
		phone_password.setSelection(phone_password.length());

	}

	@Override
	protected void initData() {

	}

	/**
	 * 
	 */
	private void submitUserInfo() {
		final String phonenum = phonenumber.getText().toString().trim();
		final String accountstr = phonename.getText().toString().trim();
		final String password = phone_password.getText().toString().trim();

		System.out.println("url...+" + Constant.getUrl("sms", "sendCode")
				+ "&target=" + phonenum + "&type=4");
		GetRequest<BaseHttpResponse> getRequest = new GetRequest<BaseHttpResponse>(
				Constant.getUrl("sms", "sendCode") + "&target=" + phonenum
						+ "&type=4", BaseHttpResponse.class,
				new OnVolleyResponseListener<BaseHttpResponse>() {

					@Override
					public void onError(VolleyError volleyError) {
						System.out.println(",,,,,,,," + volleyError);
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(RegistAct.this, getResources()
								.getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(BaseHttpResponse response) {
						System.out.println("............" + response);
						UIUtil.DissDialog(context, waittingDialog);
						if (response.getCode() == 10000) {
							Intent intent = new Intent(RegistAct.this,
									ComfrimCodeAct.class);
							intent.putExtra("phonenum", phonenum);
							intent.putExtra("accountstr", accountstr);
							intent.putExtra("password", password);
							intent.putExtra("id", response.getId());
							startActivity(intent);
						} else {
							UIUtil.showToast(RegistAct.this,
									response.getError());
						}
					}
				});
		executeRequest(getRequest);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.fast_rigist_button:
			if (checkInfo()) {
				submitUserInfo();
			}
			break;
		case R.id.username_delete_img:
			phonename.setText("");
			break;
		// case R.id.pnone_delete_img:
		// phonenumber.setText("");
		// break;
		// case R.id.password_delete_img:
		// phone_password.setText("");
		// break;
		default:
			break;
		}
	}

	/**
	 * 判定输入汉字
	 * 
	 * @param c
	 * @return
	 */
	public boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
			return true;
		}
		return false;
	}

	/**
	 * 检测String是否全是中文
	 * 
	 * @param name
	 * @return
	 */
	public boolean checkNameChese(String name) {
		boolean res = true;
		char[] cTemp = name.toCharArray();
		for (int i = 0; i < name.length(); i++) {
			if (!isChinese(cTemp[i])) {
				res = false;
				break;
			}
		}
		return res;

	}

	private boolean checkInfo() {
		String username = phonename.getText().toString().trim();
		String phone = phonenumber.getText().toString().trim();
		String password = phone_password.getText().toString().trim();
		if (!Pattern.matches(Constant.NAME_MATCH, username)&&checkNameChese(username)) {
			UIUtil.showToast(RegistAct.this,
					getResources().getString(R.string.chect_name));
			return false;
		} else if (!Pattern.matches(Constant.PHONE_MATCH, phone)) {
			UIUtil.showToast(RegistAct.this,
					getResources().getString(R.string.chect_phone));
			return false;
		} else if (!Pattern.matches(Constant.PASSWORD, password)) {
			UIUtil.showToast(RegistAct.this,
					getResources().getString(R.string.chect_password));
			return false;
		}
		return true;
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		fast_rigist_button.setOnClickListener(this);
		// username_delete_img.setOnClickListener(this);
		// pnone_delete_img.setOnClickListener(this);
		// password_delete_img.setOnClickListener(this);

		fast_rigist_button.setClickable(false);// 把按钮 置灰色
	}

	private TextWatcher NameTextWatcher = new TextWatcher() {
		private int editEnd;
		private int editStart;
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			editStart = phonename.getSelectionStart();
			editEnd = phonename.getSelectionEnd();
			phonename.removeTextChangedListener(NameTextWatcher);
			if (Pattern.matches(Constant.NAME_MATCH, temp)) {
				chenck_name_img.setVisibility(View.VISIBLE);
				name_flag = -1;
				if (name_flag == -1 && phone_flag == -1 && password_flag == -1) {

					fast_rigist_button
							.setBackgroundResource(R.drawable.login_button_selector);
					fast_rigist_button.setClickable(true);
				} else
					fast_rigist_button.setClickable(false);
			}
			if (!Pattern.matches(Constant.NAME_MATCH, temp)) {
				name_flag = 0;
				chenck_name_img.setVisibility(View.GONE);
				fast_rigist_button.setClickable(false);
				fast_rigist_button
						.setBackgroundResource(R.drawable.login_button_press);
			}
			if (temp.length() != 0) {
				chenck_name_text.setVisibility(View.GONE);
			} else {
				chenck_name_text.setVisibility(View.VISIBLE);
			}

			phonename.setText(s);
			phonename.setSelection(editStart);

			phonename.addTextChangedListener(NameTextWatcher);
		}
	};
	private TextWatcher PhoneTextWatcher = new TextWatcher() {
		private int editEnd;
		private int editStart;
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			editStart = phonenumber.getSelectionStart();
			editEnd = phonenumber.getSelectionEnd();
			phonenumber.removeTextChangedListener(PhoneTextWatcher);
			if (Pattern.matches(Constant.PHONE_MATCH, temp)) {
				chenck_phone_img.setVisibility(View.VISIBLE);
				phone_flag = -1;
				if (name_flag == -1 && phone_flag == -1 && password_flag == -1) {
					fast_rigist_button.setClickable(true);
					fast_rigist_button
							.setBackgroundResource(R.drawable.login_button_selector);
				} else
					fast_rigist_button.setClickable(false);

			}
			if (!Pattern.matches(Constant.PHONE_MATCH, temp)) {
				phone_flag = 0;
				chenck_phone_img.setVisibility(View.GONE);
				fast_rigist_button.setClickable(false);
				fast_rigist_button
						.setBackgroundResource(R.drawable.login_button_press);
			}
			if (temp.length() != 0) {
				chenck_phone_text.setVisibility(View.GONE);
			} else {
				chenck_phone_text.setVisibility(View.VISIBLE);
			}

			phonenumber.setText(s);
			phonenumber.setSelection(editStart);

			phonenumber.addTextChangedListener(PhoneTextWatcher);
		}
	};
	private TextWatcher PsswordTextWatcher = new TextWatcher() {
		private int editEnd;
		private int editStart;
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			editStart = phone_password.getSelectionStart();
			editEnd = phone_password.getSelectionEnd();
			phone_password.removeTextChangedListener(PsswordTextWatcher);
			if (Pattern.matches(Constant.PASSWORD, temp)) {
				chenck_password_img.setVisibility(View.VISIBLE);
				password_flag = -1;
				if (name_flag == -1 && phone_flag == -1 && password_flag == -1) {
					fast_rigist_button.setClickable(true);
					fast_rigist_button
							.setBackgroundResource(R.drawable.login_button_selector);
				} else {
					fast_rigist_button.setClickable(false);

				}
			}
			if (!Pattern.matches(Constant.PASSWORD, temp)) {
				password_flag = 0;
				chenck_password_img.setVisibility(View.GONE);
				fast_rigist_button.setClickable(false);
				fast_rigist_button
						.setBackgroundResource(R.drawable.login_button_press);
			}
			if (temp.length() != 0) {
				chenck_password_text.setVisibility(View.GONE);
			} else {
				chenck_password_text.setVisibility(View.VISIBLE);
			}

			phone_password.setText(s);
			phone_password.setSelection(editStart);

			phone_password.addTextChangedListener(PsswordTextWatcher);
		}
	};
	private TextView chenck_name_text;
	private ImageView chenck_name_img;
	private TextView chenck_phone_text;
	private ImageView chenck_phone_img;
	private TextView chenck_password_text;
	private ImageView chenck_password_img;
}
